﻿
Imports std

Namespace configNaming
    Namespace raranker
        Public Module raranker
            Public Const name As String = "raranker"
            Public Const port As String = "port"
            Public Const storoomhost As String = "storoomhost"
            Public Const storoomport As String = "storoomport"
            Public Const storoomretrytimes As String = "storoomretrytimes"
            Public Const ignorecrawlingtag As String = "ignorecrawlingtag"
            Public Const maxresultcount As String = "maxresultcount"
        End Module
    End Namespace

    Namespace defaultValue
        Namespace raranker
            Public Module raranker
                Public Const port As Int32 = 32220
                Public Const storoomretrytimes As Int32 = 128
                Public Const ignorecrawlingtag As Boolean = True
                Public Const maxresultcount As Int64 = 100
            End Module
        End Namespace
    End Namespace
End Namespace

Namespace config
    Public Module config
        Public WithEvents config As configMonitor = Nothing

        Public storoomhosts As vector(Of String) = Nothing
        Public storoomports As vector(Of Int32) = Nothing
        Public storoomretrytimes As Int32 = configNaming.defaultValue.raranker.storoomretrytimes
        Public ignorecrawlingtag As Boolean = configNaming.defaultValue.raranker.ignorecrawlingtag
        Public port As Int32 = configNaming.defaultValue.raranker.port
        Public maxresultcount As Int64 = configNaming.defaultValue.raranker.maxresultcount

        Public Function configRefreshing() As Boolean
            Return config.configRefreshing()
        End Function

        Public Function open(ByRef configfile As String) As Boolean
            assert(config Is Nothing, "already open a config, should call initial if refreshing.")
            config = New configMonitor()
            If config.open(configfile) Then
                Return initial()
            Else
                Return False
            End If
        End Function

        Public Function initial() As Boolean
            assert(Not config Is Nothing, "config is nothing before initial")
            If Not config.readlist(configNaming.raranker.name, configNaming.raranker.storoomhost, storoomhosts) Then
                raiseError("cannot read list of storoomhost, failed to initialize, section " + configNaming.raranker.name _
                           + ", parameter " + configNaming.raranker.storoomhost, errorHandle.errorType.exclamation)
                Return False
            End If
            If Not config.readlist(configNaming.raranker.name, configNaming.raranker.storoomport, storoomports) Then
                raiseError("cannot read list of storoomport, failed to initialize, section " + configNaming.raranker.name _
                           + ", parameter " + configNaming.raranker.storoomport, errorHandle.errorType.exclamation)
                Return False
            End If
            If storoomhosts.size() <> storoomports.size() Then
                raiseError("count of storoomhosts <> count of storoomports, ignore some to make pairs." _
                           , errorHandle.errorType.exclamation)
                If storoomhosts.size() > storoomports.size() Then
                    storoomhosts.erase(storoomports.size(), storoomhosts.size())
                    storoomports.erase(storoomhosts.size(), storoomports.size())
                End If
            End If
            storoomretrytimes = config.parameter(configNaming.raranker.name, configNaming.raranker.storoomretrytimes _
                                                  , configNaming.defaultValue.raranker.storoomretrytimes)
            ignorecrawlingtag = config.parameter(configNaming.raranker.name, configNaming.raranker.ignorecrawlingtag _
                                                  , configNaming.defaultValue.raranker.ignorecrawlingtag)
            port = config.parameter(configNaming.raranker.name, configNaming.raranker.port _
                                     , configNaming.defaultValue.raranker.port)
            maxresultcount = config.parameter(configNaming.raranker.name, configNaming.raranker.maxresultcount _
                                                 , configNaming.defaultValue.raranker.maxresultcount)

            Return True
        End Function

        Public Function refreshing() As Boolean
            Return config.refreshing()
        End Function

        Private Sub config_ConfigChanged() Handles config.ConfigChanged
            If Not initial() Then
                raiseError("failed to load config when caught changing, shutdown", errorHandle.errorType.exclamation)
                shutdown()
            Else
                shutdown()
                startservice()
            End If
        End Sub

        Private Sub config_ConfigfileDeleted() Handles config.ConfigfileDeleted
            If configMonitor.defaultConfigDeletedOperation(60 * 60 * 1000, 60 * 60) Then
                shutdown()
            End If
        End Sub
    End Module
End Namespace
